/**
 * Copyright (C) 2014 Baidu, Inc. All Rights Reserved.
 */
package com.xl.core.util;

import java.util.List;

import org.apache.log4j.LogManager;
import org.apache.log4j.Logger;

import redis.clients.jedis.Jedis;
import redis.clients.jedis.JedisPool;
import redis.clients.jedis.JedisPoolConfig;

/**
 * 功能模块：Jedis公共类
 ********************************* 
 * @author v_lianghua
 * @date 2014-12-4 下午08:06:54
 * @version 1.0.0
 ********************************* 
 */
public class JedisUtil {

	private static final Logger logger = LogManager.getLogger(JedisUtil.class);
	// 读取配置文件
	private static PropertiesUtil UTIL = null;
	static {
		UTIL = new PropertiesUtil("jedis.properties");
	}

	// session默认存在时间：120分钟
	private static final Integer SESSION_DEFAULT_EXPIRE = Integer.valueOf(UTIL
			.readProperty("redis.default.expire"));

	private static final String KEY_PREFIX = "KEY_";
	// 连接池
	private static JedisPool jedisPool = null;

	static {
		JedisPoolConfig config = new JedisPoolConfig();
		// 设置最大连接数
		config.setMaxActive(Integer.valueOf(UTIL
				.readProperty("redis.MaxActive")));
		// 设置最大空闲数
		config.setMaxIdle(Integer.valueOf(UTIL.readProperty("redis.MaxIdle")));
		// 设置初始化数
		config.setMinIdle(Integer.valueOf(UTIL.readProperty("redis.MinIdle")));
		// 设置超时时间
		config.setMaxWait(Integer.valueOf(UTIL.readProperty("redis.MaxWait")));
		// 提前进行validate操作，确保获取的jedis实例均是可用
		config.setTestOnBorrow(Boolean.valueOf(UTIL
				.readProperty("redis.TestOnBorrow")));

		String host = UTIL.readProperty("redis.ip");
		int port = Integer.valueOf(UTIL.readProperty("redis.port"));
		int database = Integer.valueOf(UTIL.readProperty("redis.database"));

		// redis设置了密码,
	 	//UTIL.readProperty("redis.password"), database
		jedisPool = new JedisPool(config, host, port, 120000);
	}

	/**
	 * 向缓存中设置对象
	 * 
	 * @param key
	 * @param value
	 * @return
	 */
	public static boolean set(String key, Object value) {
		Jedis jedis = null;
		try {

			jedis = jedisPool.getResource();
			jedis.set(KEY_PREFIX + key, SerializeUtil.serialize(value));
			jedis.expire(KEY_PREFIX + key, SESSION_DEFAULT_EXPIRE);
			return true;
		} catch (Exception e) {
			e.printStackTrace();
			logger.debug(e.getMessage());
			return false;
		} finally {
			jedisPool.returnResource(jedis);
		}
	}

	/**
	 * 向缓存中设置对象
	 * 
	 * @param key
	 * @param value
	 * @return
	 */
	public static boolean setex(String key, int expire, Object value) {
		Jedis jedis = null;
		try {
			jedis = jedisPool.getResource();
			jedis.set(KEY_PREFIX + key, SerializeUtil.serialize(value));
			jedis.expire(KEY_PREFIX + key, expire);
			return true;
		} catch (Exception e) {
			e.printStackTrace();
			logger.debug(e.getMessage());
			return false;
		} finally {
			jedisPool.returnResource(jedis);
		}
	}

	/**
	 * 删除缓存中得对象，根据key
	 * 
	 * @param key
	 * @return
	 */
	public static boolean del(String key) {
		Jedis jedis = null;
		try {
			jedis = jedisPool.getResource();
			jedis.del(KEY_PREFIX + key);
			return true;
		} catch (Exception e) {
			e.printStackTrace();
			logger.debug(e.getMessage());
			return false;
		} finally {
			jedisPool.returnResource(jedis);
		}
	}

	/**
	 * 删除缓存中得对象，根据key
	 * 
	 * @param key
	 * @return
	 */
	public static boolean clear() {
		Jedis jedis = null;
		try {
			jedis = jedisPool.getResource();
			jedis.flushDB();
			return true;
		} catch (Exception e) {
			e.printStackTrace();
			logger.debug(e.getMessage());
			return false;
		} finally {
			jedisPool.returnResource(jedis);
		}
	}

	/**
	 * 根据key 获取内容
	 * 
	 * @param key
	 * @return
	 */
	public static Object get(String key) {
		Jedis jedis = null;
		try {
			jedis = jedisPool.getResource();
			Object value = SerializeUtil.unserialize(jedis
					.get(KEY_PREFIX + key));
			return value;
		} catch (Exception e) {
			e.printStackTrace();
			logger.debug(e.getMessage());
			return false;
		} finally {
			jedisPool.returnResource(jedis);
		}
	}

	/**
	 * 根据key 获取内容
	 * 
	 * @param key
	 * @return
	 */
	public static <T> T getObject(String key, Class<T> clazz) {
		Jedis jedis = null;
		try {
			jedis = jedisPool.getResource();
			T value = SerializeUtil.unserializeObject(
					jedis.get(KEY_PREFIX + key), clazz);
			return value;
		} catch (Exception e) {
			e.printStackTrace();
			logger.debug(e.getMessage());
			return null;
		} finally {
			jedisPool.returnResource(jedis);
		}
	}

	/**
	 * 根据key 获取内容
	 * 
	 * @param key
	 * @return
	 */
	public static <T> List<T> getList(String key, Class<T> clazz) {
		Jedis jedis = null;
		try {
			jedis = jedisPool.getResource();
			List<T> value = SerializeUtil.unserializeList(
					jedis.get(KEY_PREFIX + key), clazz);
			return value;
		} catch (Exception e) {
			e.printStackTrace();
			logger.debug(e.getMessage());
			return null;
		} finally {
			jedisPool.returnResource(jedis);
		}
	}

	/**
	 * 
	 * @function 设置过期时间
	 * @date 2014-12-4 下午08:25:51
	 * @author v_lianghua
	 * @param key
	 * @param seconds
	 * @return void
	 */
	public static Long expire(String key, int seconds) {
		if (seconds < 0) {
			return null;
		}
		Jedis jedis = null;
		try {
			jedis = jedisPool.getResource();
			return jedis.expire(KEY_PREFIX + key, seconds);
		} catch (Exception e) {
			e.printStackTrace();
			logger.debug(e.getMessage());
		} finally {
			jedisPool.returnResource(jedis);
		}
		return null;
	}

	/**
	 * 
	 * @function 的获取大小
	 * @date 2014-12-4 下午08:25:51
	 * @author v_lianghua
	 * @param key
	 * @param seconds
	 * @return void
	 */
	public static Long dbsize() {

		Jedis jedis = null;
		try {
			jedis = jedisPool.getResource();
			return jedis.dbSize();
		} catch (Exception e) {
			e.printStackTrace();
			logger.debug(e.getMessage());
		} finally {
			jedisPool.returnResource(jedis);
		}
		return null;
	}

}
